package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.PointsStore;
import com.baomidou.ant.service.IPointsStoreService;
import com.baomidou.ant.utils.VoUtilsTool;
import com.baomidou.ant.vo.Request.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/points-store")
public class PointsStoreController {
    @Autowired
    private IPointsStoreService pointsStoreService;

    //上传图片
    @RequestMapping("/upload/{goodsId}")
    public Result upload(@RequestParam("file") MultipartFile file, HttpServletRequest request, @PathVariable Integer goodsId){
        String path = pointsStoreService.upload(file);
        //保存头像路径
        HttpSession session=request.getSession();
        session.setAttribute("path",path);
        PointsStore goods =pointsStoreService.getById(goodsId);
        goods.setGoodsImage((String)session.getAttribute("path"));
        pointsStoreService.saveOrUpdate(goods);
        return Result.getSuccess().setMsg("上传成功");
    }

    //新增商品
    @PostMapping("/save")
    public Result save(@RequestBody RequestSaveGoodsVo requestSaveGoodsVo){
        //判断
        if (VoUtilsTool.checkObjFieldIsNull(requestSaveGoodsVo)){
            return Result.getFailure().setMsg("不能有空项");
        }
        PointsStore goods=new PointsStore();
        BeanUtils.copyProperties(requestSaveGoodsVo,goods);
        if(pointsStoreService.saveOrUpdate(goods)){
            return Result.getSuccess().setMsg("添加成功");
        }else{
            return Result.getFailure().setMsg("添加失败");
        }
    }

    //修改商品信息
    @PostMapping("/update")
    public Result update(@RequestBody RequestUpdateGoodsVo requestUpdateGoodsVo){
        PointsStore goods = pointsStoreService.getById(requestUpdateGoodsVo.getId());
        //判断空项，保留原值
        if(ObjectUtils.isEmpty(requestUpdateGoodsVo.getGoodsAmount())){
            requestUpdateGoodsVo.setGoodsAmount(goods.getGoodsAmount());
        }
        if(ObjectUtils.isEmpty(requestUpdateGoodsVo.getGoodsDesc())){
            requestUpdateGoodsVo.setGoodsDesc(goods.getGoodsDesc());
        }
        if(ObjectUtils.isEmpty(requestUpdateGoodsVo.getGoodsName())){
            requestUpdateGoodsVo.setGoodsName(goods.getGoodsName());
        }
        if(ObjectUtils.isEmpty(requestUpdateGoodsVo.getPrice())){
            requestUpdateGoodsVo.setPrice(goods.getPrice());
        }
        if(ObjectUtils.isEmpty(requestUpdateGoodsVo.getGoodsType())){
            requestUpdateGoodsVo.setGoodsType(goods.getGoodsType());
        }
        //修改信息并保存
        BeanUtils.copyProperties(requestUpdateGoodsVo,goods);
        if (pointsStoreService.updateById(goods)) {
            return Result.getSuccess().setMsg("修改成功");
        } else {
            return Result.getSuccess().setMsg("修改失败");
        }
    }

    //展示商品接口
    @PostMapping("/listOfUser")
    public Result list(@RequestBody RequestListGoodsVo requestListGoodsVo){
        IPage<PointsStore> iPage=pointsStoreService.list(requestListGoodsVo);
        return Result.getSuccess().setData(iPage);
    }

    //兑换接口
    @PostMapping("/exchangeGoods")
    public Result exchangeGoods(@RequestBody RequestExchangeGoodsVo requestExchangeGoodsVo){
        if(pointsStoreService.exchangeGoods(requestExchangeGoodsVo)==1){
            return Result.getSuccess().setMsg("兑换成功!");
        }else if(pointsStoreService.exchangeGoods(requestExchangeGoodsVo)==0){
            return Result.getFailure().setMsg("兑换失败!积分不足以兑换该商品，请再接再厉!");
        }else if(pointsStoreService.exchangeGoods(requestExchangeGoodsVo)==404) {
            return Result.getFailure().setMsg("兑换失败!库存不足，下次请快点兑换哦.");
        }else if(pointsStoreService.exchangeGoods(requestExchangeGoodsVo)==-3){
            return Result.getFailure().setMsg("背包已满!");
        }else if(pointsStoreService.exchangeGoods(requestExchangeGoodsVo)==-2){
            return Result.getFailure().setMsg("背包存在该唯一性道具，请使用完后再次兑换");
        }else if(pointsStoreService.exchangeGoods(requestExchangeGoodsVo)==-400) {
            return Result.getFailure().setMsg("非卖品，敬请期待。");
        }else if(pointsStoreService.exchangeGoods(requestExchangeGoodsVo)==500)
            return Result.getFailure().setMsg("已售罄");
        return Result.getFailure().setMsg("你已经兑换过了，给其他小伙伴一点机会吧.");
    }

    //通过id删除
    @DeleteMapping("/delete/{goodsId}")
    public Result delete(@PathVariable Integer goodsId){
        if(pointsStoreService.removeById(goodsId)){
            return Result.getSuccess().setMsg("删除成功!");
        }else {
            return Result.getFailure().setMsg("删除失败!");
        }
    }

    //批量
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody RequestDeleteVo requestDeleteVo){
        List<Integer> ids= Arrays.asList(requestDeleteVo.getIds());
        if(ids.size()>0&&pointsStoreService.removeByIds(ids)){
            return Result.getSuccess().setMsg("删除成功");
        }else{
            return Result.getFailure().setMsg("请勾选要删除的项！");
        }
    }

    //后台下架商品
    @PostMapping("/pullOff")
    public Result pullOffGoods(@RequestBody Integer goodsId){
        PointsStore goods=pointsStoreService.getById(goodsId);
        goods.setStatus("0");
        pointsStoreService.saveOrUpdate(goods);
        return Result.getSuccess().setMsg("下架成功");
    }
}
